package cn.zcscloud.mall.controller;

import cn.zcscloud.mall.common.PageRequest;
import cn.zcscloud.mall.common.R;
import cn.zcscloud.mall.mbg.model.PmsBrand;
import cn.zcscloud.mall.service.PmsBrandService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/brand1")
public class PmsBrandController {

    @Autowired
    private PmsBrandService pmsBrandService;

    /**
     * 添加品牌
     *
     * @param pmsBrand
     * @return
     */
    @PostMapping("/create")
    public R<String> create(@RequestBody PmsBrand pmsBrand) {
        pmsBrandService.save(pmsBrand);
        return R.success(null);
    }

    /**
     * 批量删除品牌
     *
     * @param ids
     * @return
     */
    @PostMapping("/delete/batch")
    public R<String> deleteByIds(@RequestParam List<Integer> ids) {
        pmsBrandService.deleteByIds(ids);
        return R.success(null);
    }

    /**
     * 删除品牌
     *
     * @param id
     * @return
     */
    @GetMapping("/delete/{id}")
    public R<String> delete(@PathVariable("id") Integer id) {
        pmsBrandService.deleteById(id);
        return R.success(null);
    }

    /**
     * 根据品牌名称分页获取品牌列表
     *
     * @param keyword
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/list")
    public R<PageRequest<PmsBrand>> list(String keyword, Integer pageNum, Integer pageSize) {
        //分页构造器
        IPage<PmsBrand> iPage = new Page<>(pageNum, pageSize);
        //查询构造器
        LambdaQueryWrapper<PmsBrand> lqw = new LambdaQueryWrapper<>();
        lqw.like(keyword != null, PmsBrand::getName, keyword);
        //查询并构造返回值
        pmsBrandService.page(iPage, lqw);
        PageRequest pr = PageRequest.init(iPage);
        return R.success(pr);
    }

    /**
     * 获取全部品牌列表
     *
     * @return
     */
    @GetMapping("/listAll")
    public R<List<PmsBrand>> listAll() {
        List<PmsBrand> list = pmsBrandService.list(new LambdaQueryWrapper<PmsBrand>().orderByDesc(PmsBrand::getSort));
        return R.success(list);
    }

    /**
     * 批量更新厂家制造商状态
     *
     * @param factoryStatus
     * @param ids
     * @return
     */
    @PostMapping("/update/factoryStatus")
    public R<String> updateFactoryStatus(Integer factoryStatus, @RequestParam List<Integer> ids) {
        UpdateWrapper<PmsBrand> uw = new UpdateWrapper<>();
        uw.lambda().in(PmsBrand::getId, ids);
        uw.lambda().set(PmsBrand::getFactoryStatus, factoryStatus);
        pmsBrandService.update(uw);
        return R.success(null);
    }

    /**
     * 批量更新显示状态
     *
     * @param ids
     * @param showStatus
     * @return
     */
    @PostMapping("/update/showStatus")
    public R<String> updateShowStatus(@RequestParam List<Integer> ids, Integer showStatus) {
        UpdateWrapper<PmsBrand> uw = new UpdateWrapper<>();
        uw.lambda().in(PmsBrand::getId, ids);
        uw.lambda().set(PmsBrand::getShowStatus, showStatus);
        pmsBrandService.update(uw);
        return R.success(null);
    }
}
